package com.buaa.aimall.userModule.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.buaa.aimall.Utils.Authentication.Login.UserLoginInfo;
import com.buaa.aimall.userModule.Entity.UserEntity;
import com.buaa.aimall.userModule.Mapper.UserMapper;
import com.ulisesbocchio.jasyptspringboot.annotation.EnableEncryptableProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

@Service
@EnableEncryptableProperties
public class UserServiceImpl extends ServiceImpl<UserMapper, UserEntity> implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public UserEntity getUserByUsername(String username) {
        QueryWrapper<UserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        return userMapper.selectOne(queryWrapper);
    }

    public UserEntity getCurrentUser() {
        UserLoginInfo userLoginInfo = (UserLoginInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return this.getById(userLoginInfo.getUserId());
    }
}
